package com.example.dict.GUI;

import com.example.dict.data.DictionaryStorage;
import com.example.dict.utils.Dictionary.Operations.AvlTree;
import com.example.dict.utils.Dictionary.dictionaryOperations;
import com.example.dict.utils.util.Record;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;

import static com.example.dict.data.staticFilterData.WORD_CHECKER;


public class addGUI extends JFrame {

    dictionaryOperations dictionary = AvlTree.acquire();
    DictionaryStorage ds = new DictionaryStorage();
    JTextPane input, output;
    JButton create;

    Color gray = Color.getHSBColor(0, 0, 0.949f);
    Color fontGray = Color.getHSBColor(0, 0, 0.7922f);

    int width = 512;
    int height = 250;

    public addGUI() {

        super("添加单词");

        JPanel panel = new JPanel();
        Border round = new LineBorder(gray, 25, true);
        input = new JTextPane();
        output = new JTextPane();
        create = new JButton("新增");

        /* 设置样式*/
        panel.setBackground(Color.gray);
        panel.setLayout(null);

        input.setFont(new Font("楷体", Font.BOLD, 16));
        output.setFont(new Font("楷体", Font.BOLD, 16));
        input.setForeground(fontGray);
        output.setForeground(fontGray);
        input.setText("新增单词");
        output.setText("对应翻译");

        input.setBackground(gray);
        output.setBackground(gray);

        input.setBorder(round);
        output.setBorder(round);

        panel.setBounds(0, 0, width, height);
        create.setBounds(200, 140, 100, 30);
        input.setBounds(28, 20, 210, 110);
        output.setBounds(250, 20, 210, 110);
        output.setEnabled(false);

        setLayout(null);
        panel.add(create);
        panel.add(input);
        panel.add(output);
        add(panel);

        int x = ((Toolkit.getDefaultToolkit().getScreenSize()).width - width) / 2;
        int y = ((Toolkit.getDefaultToolkit().getScreenSize()).height - height) / 2;
        setLocation(x, y);
        setSize(width, height);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);

        setVisible(true);

        create.addActionListener(e -> {

            String word = input.getText();

            if (!word.matches(WORD_CHECKER)) {
                input.setText("请输入新增的单词");
                return;
            }

            word = word.toLowerCase();

            String trans = dictionary.translate(word);

            if (null != trans) {
                JOptionPane.showMessageDialog(null, "单词已存在");
                output.setText(trans);
                return;
            }

            /* 3.2 不存在，插入获取翻译 */
            String translation = output.getText();
            if (null == translation || "".equals(translation)) {
                JOptionPane.showMessageDialog(null, "译文不能为空");
                return;
            }

            dictionary.insert(word, translation);
            System.out.println("增加完成");
            JOptionPane.showMessageDialog(null, "单词收录成功");
            try {
                ds.storage(new Record(word, translation));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
            input.setText("");
            output.setText("");
        });

        input.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                input.setText("");
                output.setText("");
                output.setEnabled(true);
                input.setForeground(Color.BLACK);
                output.setForeground(Color.BLACK);
            }
        });
    }
}
